package com.tinama.jedis01.demo;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author lgc
 * @createDate 2020/11/5 - 14:38
 */
public class Father {
    /**
     * i=test()执行的是子类重写的test,因为是子类实例化触发的父类加载
     */
    private int i = test();
    private static int j = method();

    static {
        ArrayList list = new ArrayList<God>();
        list.add(1);
        list.remove(2);

        System.out.print("(1)");
    }

    public Father() {
        System.out.print("(2)");
    }

    {
        System.out.print("(3)");
    }

    public int test() {
        System.out.print("(4)");
        return 1;
    }

    public static int method(){
        System.out.print("(5)");
        return 1;
    }



    public static void main(String[] args) throws Exception {
        int[] array = {5,9,3,6,47,89,62,85,66};
        System.out.println("倒序前：" + Arrays.toString(array));
        System.out.println("排序后：" + Arrays.toString(sort(array)));
//        System.out.println("倒序后"+Arrays.toString(descSort(array)));
//        String str = "天码星空哈哈哈是在";
//        System.out.println(descString(str));

    }

    public static String descString(String org) throws Exception {
        if (org == null || org.equals("")){
            throw new Exception("空异常");
        }
        if(org.length()<=1 ) {
            return org;
        }
       return descString(org.substring(1))+org.charAt(0);
    }
    public static int[] descSort(int[] arr) throws Exception {
        if (arr ==null || arr.length==0){
            throw new Exception("空异常");
        }

       for (int i=0,j=arr.length-1;i<j;i++,j--){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
       }
       return arr;
    }

    public static int[] sort(int[] arr) throws Exception {
        if (arr ==null || arr.length==0){
            throw new Exception("空异常");
        }
        int temp = 0;
        for (int i = 0 ; i< arr.length-1;i++){
            boolean isExchange = false;
            for (int j = 0; j < arr.length-1-i;j++){
                if (arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] =  arr[j+1];
                    arr[j+1] = temp;
                    //标记有交换的痕迹
                    isExchange = true;
                }
            }
            if (!isExchange){
                System.out.println(String.format("在第%d行已经跳出",i+1));
                break;
            }
        }
        return arr;
    }

}